package com.baizhi.service.impl;

import com.baizhi.dao.PlayedDao;
import com.baizhi.entity.Played;
import com.baizhi.feign.VideoFeign;
import com.baizhi.po.FeedVideoPO;
import com.baizhi.po.PlayedPO;
import com.baizhi.service.PlayedService;
import com.baizhi.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 播放历史(Played)表服务实现类
 *
 * @author makejava
 * @since 2021-09-27 22:26:04
 */
@Transactional
@Service("playedService")
public class PlayedServiceImpl extends ServiceImpl<PlayedDao, Played> implements PlayedService {

    private static final Logger log1 = LoggerFactory.getLogger(PlayedServiceImpl.class);
    @Autowired
    private PlayedDao playedDao;
    @Autowired
    private VideoFeign videoFeign;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;

    @Override
    public List<PlayedPO> quweyAllVideo(Integer uid, Integer page, Integer pageSize) {
        //创建分页对象
        Page<Played> pages = new Page<>(page, pageSize);
        //创建条件对象
        LambdaQueryWrapper<Played> queryWrapper = new LambdaQueryWrapper<>();
        //创建条件
        queryWrapper.eq(Played::getUid, uid);
        queryWrapper.orderByDesc(Played::getCreatedAt);
        //分页查看数据
        Page<Played> playedPage = playedDao.selectPage(pages, queryWrapper);

        //获取数据
        List<Played> records = playedPage.getRecords();

        //创建返回信息
        List<PlayedPO> playedPOS = new ArrayList<>();

        records.forEach(played -> {
            //创建单个对象
            PlayedPO playedPO = new PlayedPO();
            Integer videoId = played.getVideoId();
            FeedVideoPO feedVideoPO = videoFeign.queryByVideosId(videoId);
            BeanUtils.copyProperties(feedVideoPO, playedPO);
            //点赞量：视频id+所有数据条数
            HashOperations hashOperations = redisTemplate.opsForHash();
            Long size = hashOperations.size("video_like_id:" + videoId);
            log1.info("video_like_id中有多少key {}", size);
            //up主：用户id到用户中获取名字
            String up = userService.queryByUid(played.getUid());
            int likes = size.intValue();
            playedPO.setLikes(likes);
            playedPO.setUploader(up);
            log1.info("playedPO {}", playedPO);
            //添加进集合中
            playedPOS.add(playedPO);
        });
        return playedPOS;
    }

    @Override
    public void add(Integer uid, Integer videoId) {
        Played entity = new Played();
        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        entity.setUid(uid);
        entity.setVideoId(videoId);
        playedDao.insert(entity);
    }
}

